/*
 * Copyright (c) 2007-2020 西安交通信息投资营运有限公司 版权所有
 * 作者:李蕊
 */

import React, {FC, useCallback, useMemo,} from 'react';
import useList, {ListOperate} from '@/hooks/useList';
import {Button, Input} from 'antd';
import service from './service';
import {SysSeqModel} from './data';
import Details from './components/Details';
import Edit from './components/Edit';
import useDelete from "@/hooks/useDelete";
import ProTableCrud, {ProTableCrudOperate} from "@/components/ProTableCrud";
import {ProColumns} from "@ant-design/pro-table/lib/Table";
import {hasPermission} from "@/utils/utils";
import config from "@/config";
import useDialog from "@/hooks/useDialog";
import Permission from "@/components/Permission";

/** 系统序列模块 */
const seqPage: FC = () => {
  const editOperate = useDialog();
  const detailsOperate = useDialog();


  //List
  const listOperate = useList<SysSeqModel>({
    onLoad: (params) => service.selectPageList(params),
    onExport: (params) => service.export(params),
  });

  //Delete
  const deleteOperate = useDelete<SysSeqModel>({
    onDelete: (ids, _) => service.delete(ids.join()),
    onDeleteSucess: listOperate.handleReload,
  });
//   //Edit
//   const editOperate = useEdit<SysSeqModel>({
//     onCreate: () => ({startWith:1,incrementBy:1}),
//     onEdit: (record) => service.selectById(record.id),
//     onSubmit: (_isCreate, _, record) => (_isCreate ? service.insert(record) : service.update(record)),
//     onSubmitSucess: listOperate.handleReload,
// });

  // //Details
  // const detailsOperate = useDetails<SysSeqModel>({
  //   onDetails: (r) => service.selectById(r.id),
  // });

  const searchOptionRender = useCallback(({searchText, resetText}, {form}) => {
    return (
      <>
        <Button type="primary" htmlType="submit" onClick={() => form.submit()}>
          <i className='fal fa-search'/> {searchText}
        </Button>
      </>
    );
  }, [listOperate]);
//表格列
  const cols = useMemo(() => getCols(listOperate), []);

  //增删改查选项
  const tableOperate: ProTableCrudOperate<SysSeqModel> = {
    cols,
    showCreate: hasPermission(config.permission.sysSeq.insert),
    showEdit: hasPermission(config.permission.sysSeq.update),
    showDelete: hasPermission(config.permission.sysSeq.delete),
    showDetails: hasPermission(config.permission.sysSeq.select),
    showExport: hasPermission(config.permission.sysSeq.export),
    listOperate,
    editOperate,
    deleteOperate,
    detailsOperate,
    getToolbarItems: (_, __) => ['create', 'edit', 'delete', 'export',],
    getEditElement: (props) => (<Edit {...props}/>),
    getDetailsElement: (props) => (<Details {...props}/>),
  };

  return (
    <Permission code={config.permission.sysSeq.select}>
      <ProTableCrud
        headerTitle="系统序列列表"
        scroll={{x: 1200}}
        operate={tableOperate}
        search={{optionRender: searchOptionRender,}}
      />
    </Permission>
  );
};

/** 获取表格列 */
function getCols(listOperate: ListOperate<SysSeqModel>): (ProColumns<SysSeqModel>[]) {
  {
    return [
      {
        title: '关键字',
        dataIndex: 'name1',
        key: 'name',
        hideInTable: true,
        renderFormItem: (item, {type, defaultRender, ...rest}) => {
          return (<Input {...rest} placeholder="请输入编码/名称"/>)
        },
      },
      {
        title: '名称',
        dataIndex: 'name',
        hideInTable: false,
        hideInSearch: true,
        ellipsis: true,
        sorter: false,
        width: 200,
        formItemProps: {
          'placeholder': '请输入序列名称',
        },
      },
      {
        title: '编码',
        dataIndex: 'code',
        hideInTable: false,
        hideInSearch: true,
        ellipsis: true,
        sorter: false,
        width: 200,
        formItemProps: {
          'placeholder': '请输入序列编码',
        },
      },
      {
        title: '开始值',
        dataIndex: 'startWith',
        hideInTable: false,
        hideInSearch: true,
        ellipsis: false,
        width: 120,
        sorter: false,
        formItemProps: {
          'placeholder': '请输入开始值',
        },
      },
      {
        title: '步长',
        dataIndex: 'incrementBy',
        hideInTable: false,
        hideInSearch: true,
        ellipsis: false,
        width: 120,
        sorter: false,
        formItemProps: {
          'placeholder': '请输入步长',
        },
      },
      {
        title: '当前值',
        dataIndex: 'currentValue',
        hideInTable: false,
        hideInSearch: true,
        ellipsis: false,
        width: 200,
        sorter: false,
        formItemProps: {
          'placeholder': '请输入当前值',
        },
      },
      {
        title: '备注',
        dataIndex: 'remark',
        hideInTable: false,
        hideInSearch: true,
        ellipsis: true,
        sorter: false,
        formItemProps: {
          'placeholder': '请输入备注',
        },
      }
    ]
  }
}

export default seqPage;

